package util;

import java.util.ArrayList;
import java.util.List;

import data.Point;

/**
 * @author lazyman
 * 记录两点间所有的点
 */
public class LineBwtTwoPoint {
	private Point start;
	private Point end;

	public List<Point> list;

	public LineBwtTwoPoint(Point start, Point end) {
		this.start = start;
		this.end = end;
		list = new ArrayList<Point>();
	}

	public void GetPath() {
		Point nowPoint = start;
		//允许斜率在1+-ratio的范围内
		float ratio = (float) 0.3;
		int riseX = 1;
		int riseY = 1;
		if(nowPoint.getX() > end.getX()){
			riseX = -riseX;
		}
		if(nowPoint.getY() > end.getY()){
			riseY = -riseY;
		}

		while (true) {
			if (nowPoint.equals(end)) {
				break;
			}
			if(end.getY() == nowPoint.getY()){
				nowPoint = new Point(nowPoint.getX() + riseX, nowPoint.getY());
			}else if(end.getX() == nowPoint.getX()){
				nowPoint = new Point(nowPoint.getX(), nowPoint.getY() + riseY);
			}else{
				float k = (float) (end.getY() - nowPoint.getY()) / ((float) (nowPoint.getX() - end.getX()));
				k = Math.abs(k);
				if(k < 1 - ratio)
				{
					nowPoint = new Point(nowPoint.getX() + riseX, nowPoint.getY());
				}else if(k > 1 + ratio)
				{
					nowPoint = new Point(nowPoint.getX(), nowPoint.getY() + riseY);
				}else{
					nowPoint = new Point(nowPoint.getX() + riseX, nowPoint.getY() + riseY);
				}
			}
			if(!nowPoint.equals(start) && !nowPoint.equals(end)) {
				list.add(nowPoint);
			}
		}
	}
	
}
